package com.qf.j2205.controller;


import com.alibaba.fastjson.JSON;
import com.qf.j2205.entity.AdminInfo;
import com.qf.j2205.service.AdminInfoService;
import com.qf.j2205.service.impl.AdminInfoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @ClassName AdminServlet
 * @Description TODO
 * @Author Windy
 * @Date 2022/8/15 17:36
 * @Version 1.0
 **/
public class AdminServlet extends ServletBase {

    /**
     * 登陆验证
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        AdminInfoService service = new AdminInfoServiceImpl();
        AdminInfo login = service.isLogin(username, password);
        if (login != null) {
            HttpSession session = req.getSession();
            session.setAttribute("admin_user",login);
            resp.sendRedirect("/main/Main.html");
        }else {
            resp.sendRedirect("/");
        }


    }

    /**
     * 退出登陆
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void logOut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        HttpSession session = req.getSession();
        session.removeAttribute("admin_user");
        resp.sendRedirect("Login.html");

    }
    /**
     * 从session中获取用户信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void getSession(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        AdminInfo admin_user = (AdminInfo)req.getSession().getAttribute("admin_user");
        String s = JSON.toJSONString(admin_user);
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.println(s);
        writer.flush();
        writer.close();
    }

    /**
     * 更新用户信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void updateAdmin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
       //获取数据
        AdminInfo admin_user = (AdminInfo)req.getSession().getAttribute("admin_user");
        int aid = admin_user.getAid();
        String aUsername = req.getParameter("txtA_user");
        String aType = req.getParameter("ddlA_type");
        String paw = req.getParameter("txtA_pass");
        String name = req.getParameter("txtA_name");
        int roleId = Integer.parseInt(req.getParameter("ddlA_post"));
        AdminInfo adminInfo = new AdminInfo(aid, aUsername, paw, name, roleId, aType);

        AdminInfoService service = new AdminInfoServiceImpl();
        boolean b = service.updateAdmin(adminInfo);
        if (b){
            resp.sendRedirect("/main/shouye.html");
        }else {
            resp.sendRedirect("error.html");
        }
    }
}
